﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccesLayer
{
    public class ReviewsDataAccesLayer
    {

        public IEnumerable <Review > GetReviews()
        {
            return new DataAccesLayer.eQualDataContext().Reviews.ToList();
        }

        public System.Collections.IEnumerable GetReviewTypes()
        { 
            return new DataAccesLayer.eQualDataContext().ReviewTypes .ToList();
        }

        public void AddNewReview(object review)
        {
            eQualDataContext DataContext = new eQualDataContext();

            if (DataContext.Reviews.Contains(review as Review)) return;

            DataContext.Reviews.InsertOnSubmit(review as Review);
            DataContext.SubmitChanges();
        }

        public System .Collections .IEnumerable  GetParticipants(object review)
        {
            Review _review = review as Review;
  
            eQualDataContext DataContext = new eQualDataContext();

            var participants = from e in DataContext.Employees
                               from e_r in DataContext.Employee_Participateds
                               where e.id == e_r.EmployeeId
                               where e_r.ReviewId == _review.ReviewId
                               select e;

            return participants.ToList();

        }


        public void AddNewParticipantToReview(object review, object employee)
        {
            
            Review _review = review as Review;
            Employee _employee = employee as Employee ;

            if (_review == null || _employee == null) return;

            Employee_Participated participation = new Employee_Participated();
            participation.EmployeeId = _employee.id;
            participation.ReviewId = _review.ReviewId;

            eQualDataContext DataContext = new eQualDataContext();

            DataContext.Employee_Participateds.InsertOnSubmit(participation);

            DataContext.SubmitChanges();

        }

        public System.Collections.IEnumerable GetReviewSubjects(object review)
        {
            Review _review = review as Review;

            return GetReviewSubjects(_review.ReviewType);
        }

        public System.Collections.IEnumerable GetReviewSubjects(int _reviewType)
        {
            eQualDataContext DataContext = new eQualDataContext();

            var reviewSubjects = from r in DataContext.ReviewSubjects
                                 where r.ReviewType == _reviewType
                                 select r;

            return reviewSubjects.ToList();
        }

        public System.Collections.IEnumerable GetDecisionsOfReview(object review)
        {
            Review _review = review as Review;


            eQualDataContext DataContext = new eQualDataContext();

            var reviewsDesicions = from d in DataContext.ReviewsSubjectDecisions
                                   where _review.ReviewId == d.Review
                                   select d;

            return reviewsDesicions.ToList();
        }

        public void AddNewDecisionToReview(object review, object reviewSubjectDecision)
        {
            Review _review = review as Review;

            if (_review == null) return;

            ReviewsSubjectDecision _reviewSubjectDecision = reviewSubjectDecision as ReviewsSubjectDecision;

            eQualDataContext DataContext = new eQualDataContext();

            _reviewSubjectDecision.Review = _review.ReviewId;
            DataContext.ReviewsSubjectDecisions.InsertOnSubmit(_reviewSubjectDecision);

            DataContext.SubmitChanges();

        }
    }
}
